package com.sjgl.esdn.service.impl;

import com.sjgl.esdn.domain.entity.Column;
import com.sjgl.esdn.mapper.ColumnMapper;
import com.sjgl.esdn.mapper.FileMapper;
import com.sjgl.esdn.service.ColumnService;
import com.sjgl.esdn.service.FileService;
import com.sjgl.esdn.service.UserService;
import com.sjgl.esdn.utils.ImageUtilsESDN;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 专栏服务实现
 *
 * @author 缪恒铭
 * @date 2022/08/11
 */
@Service
public class ColumnServiceImpl implements ColumnService {
    @Autowired
    ColumnMapper columnMapper;

    @Autowired
    FileMapper fileMapper;

    @Override
    public int insertColumn(Long userId, String columnName, String columnIntro) {
        //判断专栏是否存在
        if (!checkColumnUnique(userId, columnName)) {
            return -1;
        }
        //插入数据
        return columnMapper.insertColumn(userId, columnName, columnIntro);
    }

    @Override
    public int deleteColumn(Long columnId) {
        return columnMapper.deleteColumn(columnId);
    }

    @Override
    public int updateColumn(Long columnId, String columnName, String columnIntro, Long userId) {
        //命名冲突
        if (!checkColumnUnique(columnMapper.getUserId(columnId), columnName)
                && !Objects.equals(columnId, selectColumnIdByUserIdAndColumnName(userId, columnName))) {
            return -1;
        }
        return columnMapper.updateColumn(columnId, columnName, columnIntro);
    }

    @Override
    public Column selectColumnById(Long columnId) {
        return columnMapper.selectColumnById(columnId);
    }

    @Override
    public List<Long> selectFileIdListById(Long columnId) {
        return columnMapper.selectFileIdListById(columnId);
    }

    @Override
    public List<Long> selectColumnIdListByUserId(Long userId) {
        return columnMapper.selectColumnIdListByUserId(userId);
    }

    @Override
    public List<Column> selectColumnListByColumnIdList(List<Long> columnIdList) {
        List<Column> columnList = new ArrayList<>();
        for (Long columnId : columnIdList) {
            Column column = columnMapper.selectColumnById(columnId);
            column.setFileNum(columnMapper.getFileNum(column.getColumnId()));
            columnList.add(column);
        }
        return columnList;
    }

    @Override
    public List<Column> selectColumnList() {
        List<Column> columnList = columnMapper.selectColumnList();
        for (Column column : columnList) {
            column.setFileNum(columnMapper.getFileNum(column.getColumnId()));
//            column.setPreview(ImageUtilsESDN.fileToBase64(getPreviewPath(column.getColumnId())));
        }
        return columnList;
    }

    @Override
    public String getPreviewPath(Long columnId) {
        //待修改
        return "D:\\ESDN\\files\\columnImage\\0.jpg";
    }


    @Override
    public int addFile(Long columnId, Long fileId) {
        fileMapper.collectionNumAdd(fileId);
        return columnMapper.addFile(columnId, fileId);
    }

    @Override
    public int deleteFile(Long columnId, Long fileId) {
        return columnMapper.deleteFile(columnId, fileId);
    }

    @Override
    public boolean checkColumnUnique(Long userId, String columnName) {
        return columnMapper.checkColumnUnique(userId, columnName) == 0;
    }

    @Override
    public boolean isColumnAndFileExist(Long columnId, Long fileId) {
        return columnMapper.isColumnAndFileExist(columnId, fileId) == 1;
    }

    @Override
    public Long getUserId(Long columnId) {
        return columnMapper.getUserId(columnMapper.getUserId(columnId));
    }

    @Override
    public Long selectColumnIdByUserIdAndColumnName(Long userId, String columnName) {
        return columnMapper.selectColumnIdByUserIdAndColumnName(userId, columnName);
    }
}
